package api

import (
	"github.com/gogf/gf/v2/encoding/gjson"
)

type PageLstReq struct {
	Page     int         `p:"page" v:"page @integer|min:1#页码必填" d:"1" dc:"页码"`            // 页码
	LimitNum int         `p:"limit" v:"pageSize @integer|min:1#每页数量必填"  d:"10" dc:"每页数量"` // 每页数量
	Keywords string      `p:"keywords" dc:"模糊搜索内容"`                                       // 搜索内容
	Wheres   *gjson.Json `p:"wheres" dc:"搜索条件" d:"{}"`
	Order    *gjson.Json `p:"order" dc:"排序条件" d:"{}"`
}

func NewPageLstReq() *PageLstReq {
	return &PageLstReq{
		Page:     1,
		LimitNum: 10,
		Keywords: "",
		Wheres:   gjson.New("{}"),
		Order:    gjson.New("{}"),
	}
}

func (r *PageLstReq) Offset() int {
	if r.Page == 0 {
		r.Page = 1
	}
	return (r.Page - 1) * r.Limit()
}

func (r *PageLstReq) Limit() int {
	if r.LimitNum == 0 {
		r.LimitNum = 10
	}
	return r.LimitNum
}

func (r *PageLstReq) SearchStr() string {
	return "%" + r.Keywords + "%"
}

func (r *PageLstReq) SearchStrM() string {
	return "/" + r.Keywords + "/"
}

func (r *PageLstReq) SearchStrOther(str string) string {
	return "%" + str + "%"
}
func NewPageLstRes[T any]() *PageLstRes[T] {
	return &PageLstRes[T]{
		Total: 0,
		List:  make([]T, 0),
	}
}

type PageLstRes[T any] struct {
	Total int `json:"total" dc:"总数"` // 总数
	List  []T `json:"list" dc:"列表"`  // 列表
}
